package com.zixuan.gulicollege.eduservice.controller;

import com.zixuan.gulicollege.common_utils.R;
import com.zixuan.gulicollege.eduservice.service.EduTeacherService;
import com.zixuan.gulicollege.entity.eduservice.EduTeacher;
import com.zixuan.gulicollege.entity.eduservice.vo.TeacherQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 讲师 前端控制器
 *
 * @author zixuan
 * @since 2021-04-28
 */
@RestController
@RequestMapping("/edu_service/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    /**
     * 获取所有讲师
     */
    @GetMapping("/findAll")
    public R getAll(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    /**
     * 逻辑删除讲师
     * @param id 通过路径获取id值 PathVariable获取Get请求上路径的值
     */
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id){
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 分页查询讲师方法
     * @param current 当前页
     * @param limit 每页记录数
     */
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(
            @PathVariable Integer current,
            @PathVariable Integer limit){
       Map<String, Object> map = teacherService.pageListTeacher(current, limit);
        return R.ok().data(map);
    }

    /**
     * 按条件查询分页
     * @param current 当前页
     * @param limit 每页记录数
     * @param teacherQuery 封装对象
     */
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(
            @PathVariable Integer current,
            @PathVariable Integer limit,
            @RequestBody(required = false) TeacherQuery teacherQuery
            ){
        Map<String, Object> map = teacherService.pageTeacherCondition(current, limit, teacherQuery);
        return R.ok().data(map);
    }

    /**
     * 添加讲师
     * @param eduTeacher
     * @return
     */
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);

        if (save){
            return R.ok();
        }
        return R.error();
    }


    /**
     * 按ID查询讲师
      * @param id
     * @return
     */
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    /**
     * 修改讲师信息
     * @param eduTeacher
     * @return
     */
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        }
        return R.error();
    }
}

